from PyQt6.QtWidgets import QApplication, QHBoxLayout, QLabel, QSizePolicy, QListWidgetItem, QWidget, QFrame
from PyQt6.QtCore import Qt
from PyQt6 import QtCore, QtGui
from again.Welcome.GeneralSettings import Ui_GeneralSettings
import sys
import logging
import picture.resources_rc  # 确保资源文件正确导入

class MenuItem(QWidget):
    def __init__(self, text, icon):
        super(MenuItem, self).__init__()
        lay = QHBoxLayout()
        labelIcon = QLabel()
        labelIcon.setPixmap(QtGui.QPixmap(icon))
        labelIcon.setSizePolicy(QSizePolicy.Policy.Fixed, QSizePolicy.Policy.Preferred)
        labelIcon.setContentsMargins(5, 0, 5, 0)
        lay.addWidget(labelIcon)
        labelItemName = QLabel(text)
        labelItemName.setAlignment(Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)
        labelItemName.setStyleSheet('''
            font-family: "Microsoft YaHei";
            font-weight: 400;
            font-size: 14px;
            color: #dcefff;
            border: 1px solid rgba(0, 0, 0, 0);
        ''')
        lay.addWidget(labelItemName)
        labelIndicator = QLabel()
        labelIndicator.setPixmap(QtGui.QPixmap(":/右箭头.png"))
        labelIndicator.setSizePolicy(QSizePolicy.Policy.Fixed, QSizePolicy.Policy.Preferred)
        lay.addWidget(labelIndicator)
        lay.setSpacing(0)
        self.setLayout(lay)
        self.icon = labelIcon

    def setIcon(self, icon):
        self.icon.setPixmap(QtGui.QPixmap(icon))


class GeneralSettingsWin(QWidget):
    signalClose = QtCore.pyqtSignal()

    def __init__(self):
        super(GeneralSettingsWin, self).__init__()
        self.ui = Ui_GeneralSettings()
        self.ui.setupUi(self)

        self.ui.listWidgetMenu.setFixedWidth(180)
        self.ui.tBtnClose.clicked.connect(self.close)
        # 隐藏窗体顶部
        self.setWindowFlags(QtCore.Qt.WindowType.FramelessWindowHint)
        self.ui.frame.setFrameStyle(QFrame.Shape.Panel)

        # 设置鼠标追踪
        self.setMouseTracking(True)

        # 设置拖动相关变量
        self.is_maximized = False
        self.initDrag()

        # 记录初始位置和大小
        self.initial_geometry = self.geometry()

        self.initUI()
        self.signalConnect()

    def initUI(self):
        self.menuItemBaseSetting = self.addMenuItem('基本设置', ":/基本设置.png")
        self.menuItemPathSetting = self.addMenuItem('路径设置', ":/路径设置.png")
        self.menuItemUpdateSetting = self.addMenuItem('升级设置', ":/升级设置.png")
        self.menuItemPlanSetting = self.addMenuItem('用户体验改善计划', ":/用户体验改善计划.png")
        self.menuItemNoticeSetting = self.addMenuItem('声音提醒', ":/声音提醒.png")
        self.ui.listWidgetMenu.setCurrentRow(0)

    def signalConnect(self):
        self.ui.listWidgetMenu.currentRowChanged.connect(self.scrollToSetting)

    def addMenuItem(self, text, icon):
        item = QListWidgetItem()
        self.ui.listWidgetMenu.addItem(item)
        item.setSizeHint(QtCore.QSize(100, 60))
        menuItem = MenuItem(text, icon)
        self.ui.listWidgetMenu.setItemWidget(item, menuItem)
        return menuItem

    def resetMenuIcon(self):
        self.menuItemBaseSetting.setIcon(":/基本设置.png")
        self.menuItemPathSetting.setIcon(":/路径设置.png")
        self.menuItemUpdateSetting.setIcon(":/升级设置.png")
        self.menuItemPlanSetting.setIcon(":/用户体验改善计划.png")
        self.menuItemNoticeSetting.setIcon(":/声音提醒.png")

    def scrollToSetting(self, row):
        if row == 0:
            self.ui.scrollArea.verticalScrollBar().setSliderPosition(self.ui.widgetBaseSettingTitle.pos().y())
            self.resetMenuIcon()
            self.menuItemBaseSetting.setIcon(":/基本设置-选中.png")
        elif row == 1:
            self.ui.scrollArea.verticalScrollBar().setSliderPosition(self.ui.widgetPathSettingTitle.pos().y())
            self.resetMenuIcon()
            self.menuItemPathSetting.setIcon(":/路径设置-选中.png")
        elif row == 2:
            self.ui.scrollArea.verticalScrollBar().setSliderPosition(self.ui.widgetUpdateSettingTitle.pos().y())
            self.resetMenuIcon()
            self.menuItemUpdateSetting.setIcon(":/升级设置-选中.png")
        elif row == 3:
            self.ui.scrollArea.verticalScrollBar().setSliderPosition(self.ui.widgetPlanSettingTitle.pos().y())
            self.resetMenuIcon()
            self.menuItemPlanSetting.setIcon(":/用户体验改善计划-选中.png")
        elif row == 4:
            self.ui.scrollArea.verticalScrollBar().setSliderPosition(self.ui.widgetNoticeSettingTitle.pos().y())
            self.resetMenuIcon()
            self.menuItemNoticeSetting.setIcon(":/声音提醒-选中.png")

    def closeEvent(self, event) -> None:
        self.signalClose.emit()
        logging.info("[GeneralSettingsWin.closeEvent] GeneralWindow is closed")


# 窗口顶部的逻辑关闭、拖拽
    def initDrag(self):
        # 设置鼠标跟踪判断扳机默认值
        self.moveDrag = False

    def mousePressEvent(self, event):
        if event.button() == Qt.MouseButton.LeftButton:
            # 判断点击位置是否在标题栏内
            if self.ui.widgetTitle.geometry().contains(event.pos()):
                self.moveDrag = True
                self.moveDragPosition = event.globalPosition().toPoint() - self.pos()

    def mouseMoveEvent(self, event):
        if self.moveDrag:
            # 移动窗口到新的位置
            self.move(event.globalPosition().toPoint() - self.moveDragPosition)
            event.accept()

    def mouseReleaseEvent(self, event):
        self.moveDrag = False


if __name__ == '__main__':
    # 创建应用程序对象
    app = QApplication(sys.argv)

    # 创建主窗口并显示
    myWin = GeneralSettingsWin()
    myWin.show()

    # 进入应用程序的主事件循环
    sys.exit(app.exec())

